drop if age==1

gen samepid=1 if wave1pid==wave2pid
replace samepid=0 if wave1pid!=wave2pid
replace samepid=. if wave1pid==.
replace samepid=. if wave2pid==.


***Control Variables
gen age01=age
recode age01 1=. 2=0 3=.25 4=.5 5=.75 6=1

gen woman=gender 
recode woman 1=0 2=1 3=. 4=.

gen white=race 
recode white 1=1 2=0 3=0 4=0 5=0 6=0

gen black=race
recode black 1=0 2=1 3=0 4=0 5=0 6=0

gen hispanic=race
recode hispanic 1=0 2=0 3=0 4=1 5=0 6=0

gen education01=education 
recode education01 1=0 2=.25 3=.5 4=.5 5=.75 6=1

gen income01=(income-1)/4 

gen pidx2=wave2pid
recode pidx2 1=1 2=.5 3=0 4=. 5=0 6=.5 7=1


**Reverse Coding OutGroup Traits
gen fliphypocout=hypocritical_outgroup
gen flipselfishout=selfish_outgroup
gen flipmeanout=mean_outgroup

recode fliphypocout 1=5 2=4 3=3 4=2 5=1
recode flipselfishout 1=5 2=4 3=3 4=2 5=1
recode flipmeanout 1=5 2=4 3=3 4=2 5=1

gen flipmarry_outgroup = 5-marry_outgroup


**Reverse Coding In Group Traits
gen fliphypocin=hypocritical_ingroup
gen flipselfishin=selfish_ingroup
gen flipmeanin=mean_ingroup

recode fliphypocin 1=5 2=4 3=3 4=2 5=1
recode flipselfishin 1=5 2=4 3=3 4=2 5=1
recode flipmeanin 1=5 2=4 3=3 4=2 5=1

gen flipmarry_ingroup = 5-marry_ingroup

**Putting Everything on a 0-1 Scale for Outgroup
replace feelingtherm_outgroup=(feelingtherm_outgroup/100)
replace patriotic_outgroup= (patriotic_outgroup-1)/4
replace intelligent_outgroup= (intelligent_outgroup-1)/4
replace honest_outgroup= (honest_outgroup-1)/4
replace openminded_outgroup=(openminded_outgroup-1)/4
replace generous_outgroup =(generous_outgroup -1)/4
replace fliphypocout =(fliphypocout-1)/4
replace flipselfishout= (flipselfishout-1)/4
replace flipmeanout =(flipmeanout-1)/4
replace trust_outgroup= (trust_outgroup-1)/4
replace friends_outgroup =(friends_outgroup-1)/3
replace neighbors_outgroup= (neighbors_outgroup-1)/3
replace flipmarry_outgroup=(flipmarry_outgroup-1)/3

**Checking the scale and putting it together
alpha feelingtherm_outgroup patriotic_outgroup intelligent_outgroup honest_outgroup openminded_outgroup generous_outgroup fliphypocout flipselfishout flipmeanout trust_outgroup friends_outgroup neighbors_outgroup flipmarry_outgroup
cor feelingtherm_outgroup patriotic_outgroup intelligent_outgroup honest_outgroup openminded_outgroup generous_outgroup fliphypocout flipselfishout flipmeanout trust_outgroup friends_outgroup neighbors_outgroup flipmarry_outgroup
gen allout=(feelingtherm_outgroup+patriotic_outgroup+intelligent_outgroup+honest_outgroup+openminded_outgroup+generous_outgroup+fliphypocout+flipselfishout+flipmeanout+trust_outgroup+friends_outgroup+neighbors_outgroup+flipmarry_outgroup)/13

**All the Terms (Out Group)
gen terms_out=(patriotic_outgroup+intelligent_outgroup+honest_outgroup+openminded_outgroup+generous_outgroup+fliphypocout+flipselfishout+flipmeanout)/8

**Social Dist (Out Group)
gen socialdist_out=(friends_outgroup+neighbors_outgroup+flipmarry_outgroup)/3

**0-1 scale for ingroup
replace feelingtherm_ingroup=(feelingtherm_ingroup/100)
replace patriotic_ingroup= (patriotic_ingroup-1)/4
replace intelligent_ingroup= (intelligent_ingroup-1)/4
replace honest_ingroup= (honest_ingroup-1)/4
replace openminded_ingroup=(openminded_ingroup-1)/4
replace generous_ingroup =(generous_ingroup -1)/4
replace fliphypocin =(fliphypocin-1)/4
replace flipselfishin= (flipselfishin-1)/4
replace flipmeanin =(flipmeanin-1)/4
replace trust_ingroup= (trust_ingroup-1)/4
replace friends_ingroup =(friends_ingroup-1)/3
replace neighbors_ingroup= (neighbors_ingroup-1)/3
replace flipmarry_ingroup=(flipmarry_ingroup-1)/3
gen allin=(feelingtherm_ingroup+patriotic_ingroup+intelligent_ingroup+honest_ingroup+openminded_ingroup+generous_ingroup+fliphypocin+flipselfishin+flipmeanin+trust_ingroup+friends_ingroup+neighbors_ingroup+flipmarry_ingroup)/13

**Difference for each term
gen alldiff=((feelingtherm_ingroup-feelingtherm_outgroup)+(patriotic_ingroup-patriotic_outgroup)+(intelligent_ingroup-intelligent_outgroup)+(honest_ingroup-honest_outgroup)+(openminded_ingroup-openminded_outgroup)+(generous_ingroup-generous_outgroup)+(fliphypocin-fliphypocout)+(flipselfishin-flipselfishout)+(flipmeanin-flipmeanout)+(trust_ingroup-trust_outgroup)+(friends_ingroup-friends_outgroup)+(neighbors_ingroup-neighbors_outgroup)+(flipmarry_ingroup-flipmarry_outgroup))/13



**Generating the conditions
gen talkcond=condition
gen sortcond=condition

recode talkcond 1=0 2=1 3=2 4=3 5=0 6=1 7=2 8=3 9=0 10=1 11=2 12=3 13=.
recode sortcond 1=0 2=0 3=0 4=0 5=1 6=1 7=1 8=1 9=2 10=2 11=2 12=2 13=.

label define talkcondlab 0 "None" 1 "Rarely" 2 "Occassionally" 3 "Frequently"
label define sortcondlab 0 "None" 1 "Moderate" 2 "Sorted"

label values talkcond talkcondlab
label values sortcond sortcondlab

gen conditionnum=1 if talkcond==0 & sortcond==0
replace conditionnum=2 if talkcond==1 & sortcond==0
replace conditionnum=3 if talkcond==2 & sortcond==0
replace conditionnum=4 if talkcond==3 & sortcond==0
replace conditionnum=5 if talkcond==0 & sortcond==1
replace conditionnum=6 if talkcond==1 & sortcond==1
replace conditionnum=7 if talkcond==2 & sortcond==1
replace conditionnum=8 if talkcond==3 & sortcond==1
replace conditionnum=9 if talkcond==0 & sortcond==2
replace conditionnum=10 if talkcond==1 & sortcond==2
replace conditionnum=11 if talkcond==2 & sortcond==2
replace conditionnum=12 if talkcond==3 & sortcond==2



**Perceptions
**Constructing the Ideology Variables
gen perideo=1 if wave2pid<4 & ideology_outparty==3
replace perideo=1 if wave2pid>4 & ideology_outparty==1

replace perideo=-1 if wave2pid>4 & ideology_outparty==3
replace perideo=-1 if wave2pid<4 & ideology_outparty==1

replace perideo=0 if wave2pid!=4 & ideology_outparty==2

gen persort=perideo
recode persort 1=1 0=0 -1=0
gen permod=perideo  
recode permod 1=0 0=1 -1=0
gen perunsort=perideo  
recode perunsort 1=0 0=0 -1=1

gen actualideo=0 if political_descriptorwave3==2 & wave2pid!=4
replace actualideo=1 if political_descriptorwave3==1 & wave2pid<4
replace actualideo=1 if political_descriptorwave3==3 & wave2pid>4
replace actualideo=-1 if political_descriptorwave3==3 & wave2pid<4
replace actualideo=-1 if political_descriptorwave3==1 & wave2pid>4

gen actualsort=actualideo
gen actualmod=actualideo
gen actualunsort=actualideo
recode actualsort -1=0 0=0 1=1
recode actualmod -1=0 0=1 1=0
recode actualunsort -1=1 0=0 1=0


**Constructing Frequency Variable
gen perfreq=talk_freq_outparty
recode perfreq 1=0 2=0 3=1
gen perocc=talk_freq_outparty
recode perocc 1=0 2=1 3=0
gen perrare=talk_freq_outparty
recode perrare 1=1 2=0 3=0

gen actualfreq=talk_freqwave3
gen actualocc=talk_freqwave3
gen actualrare=talk_freqwave3
recode actualfreq 1=0 2=0 3=1
recode actualocc 1=0 2=1 3=0
recode actualrare 1=1 2=0 3=0

label values talk_freqwave3 talkcondlab

cls
**Table 1
tab sortcond talkcond 

**Table 2
regr allout i.talkcond i.sortcond if wave2pid!=4

**Figure 1 - Perceptions
mean persort permod perunsort if talkcond==. & sortcond==. & wave2pid!=4
mean actualsort actualmod actualunsort

mean perfreq perocc perrare if talkcond==. & sortcond==. & wave2pid!=4
mean actualfreq actualocc actualrare if wave2pid!=4

**Figure 2 and Supplementary Information 11: OLS model for figure 1
regr allout i.conditionnum if (conditionnum==1 | conditionnum==6 | conditionnum==12) & wave2pid!=4
margins conditionnum

**Supplementary Information 1: Results for each type of affective polarization measure
tab age
tab gender 
tab race
tab income
tab education

**Supplementary Information 3: Perceptions and actual levels of Democrats and Republicans
**Democrats
mean persort permod perunsort if talkcond==. & sortcond==. & wave2pid<4
mean actualsort actualmod actualunsort if wave2pid<4

mean perfreq perocc perrare if talkcond==. & sortcond==. & wave2pid<4
mean actualfreq actualocc actualrare if wave2pid<4

**Republicans
mean persort permod perunsort if talkcond==. & sortcond==. & wave2pid>4
mean actualsort actualmod actualunsort if wave2pid>4

mean perfreq perocc perrare if talkcond==. & sortcond==. & wave2pid>4
mean actualfreq actualocc actualrare if wave2pid>4

cls

**Manipulation Check and Benchmarking (Supplementary Information 4)
tab category_outgroup 
tab category_ingroup
corr category_outgroup category_ingroup

tab category_outparty 
tab category_party
corr category_outparty category_party

*(to compute means on original scales --> multiply therm*100, multiply terms (traits) * 4 and add 1, multiply trust * 4 and add 1, multiply social distance * 3 and add 1
sum feelingtherm_outgroup terms_out trust_outgroup socialdist_out if condition == 1 & wave2pid!=4


**Supplementary Information 6: Differences in In-Party and Out-Party Ratings
regr alldiff i.talkcond i.sortcond if wave2pid!=4

**Supplementary Information 7: Results for each type of affective polarization measure
regr feelingtherm_outgroup i.talkcond i.sortcond if wave2pid!=4
regr terms i.talkcond i.sortcond if wave2pid!=4
regr trust_outgroup i.talkcond i.sortcond if wave2pid!=4
regr socialdist_out i.talkcond i.sortcond if wave2pid!=4
 
**Supplementary Information 8: Table 2’s model with control variables.
regr allout i.talkcond i.sortcond age01 woman white black hispanic education01 income01 pidx2

cls

**Supplementary Information 9: Condition-by-condition comparisons
ttest allout if conditionnum==1 | conditionnum==2, by(conditionnum)
ttest allout if conditionnum==1 | conditionnum==3, by(conditionnum)
ttest allout if conditionnum==1 | conditionnum==4, by(conditionnum)
ttest allout if conditionnum==1 | conditionnum==5, by(conditionnum)
ttest allout if conditionnum==1 | conditionnum==6, by(conditionnum)
ttest allout if conditionnum==1 | conditionnum==7, by(conditionnum)
ttest allout if conditionnum==1 | conditionnum==8, by(conditionnum)
ttest allout if conditionnum==1 | conditionnum==9, by(conditionnum)
ttest allout if conditionnum==1 | conditionnum==10, by(conditionnum)
ttest allout if conditionnum==1 | conditionnum==11, by(conditionnum)
ttest allout if conditionnum==1 | conditionnum==12, by(conditionnum)

ttest allout if conditionnum==2 | conditionnum==3, by(conditionnum)
ttest allout if conditionnum==2 | conditionnum==4, by(conditionnum)
ttest allout if conditionnum==2 | conditionnum==5, by(conditionnum)
ttest allout if conditionnum==2 | conditionnum==6, by(conditionnum)
ttest allout if conditionnum==2 | conditionnum==7, by(conditionnum)
ttest allout if conditionnum==2 | conditionnum==8, by(conditionnum)
ttest allout if conditionnum==2 | conditionnum==9, by(conditionnum)
ttest allout if conditionnum==2 | conditionnum==10, by(conditionnum)
ttest allout if conditionnum==2 | conditionnum==11, by(conditionnum)
ttest allout if conditionnum==2 | conditionnum==12, by(conditionnum)

ttest allout if conditionnum==3 | conditionnum==4, by(conditionnum)
ttest allout if conditionnum==3 | conditionnum==5, by(conditionnum)
ttest allout if conditionnum==3 | conditionnum==6, by(conditionnum)
ttest allout if conditionnum==3 | conditionnum==7, by(conditionnum)
ttest allout if conditionnum==3 | conditionnum==8, by(conditionnum)
ttest allout if conditionnum==3 | conditionnum==9, by(conditionnum)
ttest allout if conditionnum==3 | conditionnum==10, by(conditionnum)
ttest allout if conditionnum==3 | conditionnum==11, by(conditionnum)
ttest allout if conditionnum==3 | conditionnum==12, by(conditionnum)

ttest allout if conditionnum==4 | conditionnum==5, by(conditionnum)
ttest allout if conditionnum==4 | conditionnum==6, by(conditionnum)
ttest allout if conditionnum==4 | conditionnum==7, by(conditionnum)
ttest allout if conditionnum==4 | conditionnum==8, by(conditionnum)
ttest allout if conditionnum==4 | conditionnum==9, by(conditionnum)
ttest allout if conditionnum==4 | conditionnum==10, by(conditionnum)
ttest allout if conditionnum==4 | conditionnum==11, by(conditionnum)
ttest allout if conditionnum==4 | conditionnum==12, by(conditionnum)

ttest allout if conditionnum==5 | conditionnum==6, by(conditionnum)
ttest allout if conditionnum==5 | conditionnum==7, by(conditionnum)
ttest allout if conditionnum==5 | conditionnum==8, by(conditionnum)
ttest allout if conditionnum==5 | conditionnum==9, by(conditionnum)
ttest allout if conditionnum==5 | conditionnum==10, by(conditionnum)
ttest allout if conditionnum==5 | conditionnum==11, by(conditionnum)
ttest allout if conditionnum==5 | conditionnum==12, by(conditionnum)

ttest allout if conditionnum==6 | conditionnum==7, by(conditionnum)
ttest allout if conditionnum==6 | conditionnum==8, by(conditionnum)
ttest allout if conditionnum==6 | conditionnum==9, by(conditionnum)
ttest allout if conditionnum==6 | conditionnum==10, by(conditionnum)
ttest allout if conditionnum==6 | conditionnum==11, by(conditionnum)
ttest allout if conditionnum==6 | conditionnum==12, by(conditionnum)

ttest allout if conditionnum==7 | conditionnum==8, by(conditionnum)
ttest allout if conditionnum==7 | conditionnum==9, by(conditionnum)
ttest allout if conditionnum==7 | conditionnum==10, by(conditionnum)
ttest allout if conditionnum==7 | conditionnum==11, by(conditionnum)
ttest allout if conditionnum==7 | conditionnum==12, by(conditionnum)

ttest allout if conditionnum==8 | conditionnum==9, by(conditionnum)
ttest allout if conditionnum==8 | conditionnum==10, by(conditionnum)
ttest allout if conditionnum==8 | conditionnum==11, by(conditionnum)
ttest allout if conditionnum==8 | conditionnum==12, by(conditionnum)

ttest allout if conditionnum==9 | conditionnum==10, by(conditionnum)
ttest allout if conditionnum==9 | conditionnum==11, by(conditionnum)
ttest allout if conditionnum==9 | conditionnum==12, by(conditionnum)

ttest allout if conditionnum==10 | conditionnum==11, by(conditionnum)
ttest allout if conditionnum==10 | conditionnum==12, by(conditionnum)

ttest allout if conditionnum==11 | conditionnum==12, by(conditionnum)

**Supplementary Information 10:  In-Party Ratings
regr allin i.talkcond i.sortcond if wave2pid!=4


**Supplementary Information 5: Pure Independents
keep allout allin wave2pid talkcond sortcond
keep if wave2pid==4

gen subid=_n

save "independentstemp.dta", replace
append using "independentstemp.dta", generate(record)

gen allscale=allout if record==0
replace allscale=allin if record==1


regr allscale i.talkcond i.sortcond, cluster(subid)





